package com.xywang.interceptor;

import com.xywang.pojo.User;
import com.xywang.util.Constants;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // System.out.println("uri: " + request.getRequestURI());
        HttpSession session = request.getSession();
        // 登录请求放行
        if (request.getRequestURI().contains("login")) {
            return true;
        }

        // 如果用户已登陆也放行
        if(session.getAttribute(Constants.USER_SESSION) != null) {
            return true;
        }

        // 用户没有登陆跳转到登陆页面
        response.sendRedirect(request.getContextPath()+"/error.jsp");
        return false;
    }

}
